﻿#region 命名空间

using System;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

#endregion

namespace WinnerCMS.Common
{

    /// <summary>
    ///     返回结果
    /// </summary>
    /// <remarks>业务处理结果</remarks>
    [DataContract]
    public class Result
    {
        private string _err = string.Empty;
        private string _message;
        private bool _success;

        /// <summary>
        ///     返回结果
        /// </summary>
        public Result()
        {
        }
        /// <summary>
        ///     返回的ID
        /// </summary>
        [DataMember(Name = "id")]
        public int Id { get; set; }

        #region 错误信息

        /// <summary>
        ///     错误信息
        /// </summary>
        [DataMember(Name = "errors")]
        public string Errors
        {
            get
            {
                if (string.IsNullOrEmpty(_err) && InnerException != null)
                {
                    return InnerException.Message;
                }
                else if (!string.IsNullOrEmpty(_err))
                {
                    return _err;
                }
                else
                {
                    return string.Empty;
                }
            }
            set { _err = value; }
        }

        #endregion

        #region 消息，非异常返回

        /// <summary>
        ///     消息，非异常返回
        /// </summary>
        [DataMember(Name = "msg")]
        public string Message
        {
            get
            {
                if (_message == null)
                {
                    return string.Empty;
                }
                return _message;
            }
            set { _message = value; }
        }

        #endregion

        #region 日志的消息

        /// <summary>
        ///     日志的消息
        /// </summary>
        [IgnoreDataMember, JsonIgnore, ScriptIgnore]
        public string LogMessage { get; set; }

        #endregion

        #region 操作是否成功

        /// <summary>
        ///     操作是否成功
        /// </summary>
        /// <remarks>Extjs 要求的</remarks>
        [DataMember(Name = "success")]
        public bool Success
        {
            get
            {
                if (Errors.Length > 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            set { this._success = value; }
        }

        #endregion


        /// <summary>
        ///     数据
        /// </summary>
        [DataMember]
        public object Data { get; set; }

        /// <summary>
        ///     异常信息
        /// </summary>
        [IgnoreDataMember, JsonIgnore, ScriptIgnore]
        public Exception InnerException { get; set; }
    }

    /// <summary>
    ///     返回结果
    /// </summary>
    /// <remarks>业务处理结果</remarks>
    [DataContract]
    public class Result<T> : Result
    {
        public Result()
        {
        }
        /// <summary>
        ///     返回结果
        /// </summary>
        public Result(T data)
        {
            Data = data;
        }

        /// <summary>
        ///     数据
        /// </summary>
        [DataMember]
        public new T Data
        {
            get { return (T)this.Data; }
            set { this.Data = value; }
        }
    }

}